// 用户相关中间件
// 导入正则校验函数
let { nameTest, ageTest, pwdTest, nickTest, emailTest } = require('../utils/regTest')
// 导入token验证方法及盐

let { veifyToken } = require('../utils/token')

// 注册中间件
let registerMid = (req, res, next) => {
  // 接受username,pwd,rpwd,email参数并进行校验
  let { username, pwd, rpwd, email } = req.body;
  // 不为空校验
  if (!username) return next({ code: 2, message: '用户名必须传递' });
  if (!pwd) return next({ code: 2, message: '密码必须传递' });
  if (!rpwd) return next({ code: 2, message: '确认密码必须传递' });
  if (!email) return next({ code: 2, message: '邮箱必须传递' });

  // 正则校验
  if (!nameTest(username)) return next({ code: 3, message: '用户名格式错误' });
  if (!pwdTest(pwd)) return next({ code: 3, message: '密码格式错误' });
  if (!emailTest(email)) return next({ code: 3, message: '邮箱格式错误' });


  next();
}

// 激活中间件
let verifyMid = (req, res, next) => {
  if (!req.query.uid) return next({ code: 0, message: '激活失败,请重试' });
  next();
}

// 登录中间件
let loginMid = (req, res, next) => {
  // 接受name,pwd参数并进行校验
  let { name, pwd } = req.body;
  // 不为空校验
  if (!name) return next({ code: 2, message: '用户名或邮箱必须传递' });
  if (!pwd) return next({ code: 2, message: '密码必须传递' });
  // 正则校验
  if (!nameTest(name) && !emailTest(name)) return next({ code: 3, message: '用户名/邮箱格式错误' });
  if (!pwdTest(pwd)) return next({ code: 3, message: '密码格式错误' });
  next()
}
// 用户详情中间件
let infoMid = (req, res, next) => {
  // 接受id参数并进行校验
  let { id } = req.query;
  // 不为空校验
  if (!id) return next({ code: 2, message: '用户id必须传递' });
  next()
}
// token校验中间件
let tokenTest = async (req, res, next) => {
  // 接受id参数并进行校验
  let id = req.query.id ?? req.body.id;
  // 接受token
  let token = req.headers.authorization
  // 不为空校验
  if (!token) return next({ code: 2, message: 'token必须传递' });

  // token校验
  let { status, info, message } = await veifyToken(token);
  if (status != 1) return next({ code: 0, message: `token失效--${message}` })
  // 解析token后 token的正确校验
  if (info.id != id) return next({ code: 0, message: `token有误` })
  next()
}

// 修改密码
let pwdMid = (req, res, next) => {
  // 接受pwd参数并进行校验
  let { oldpwd, newpwd, rnewpwd, id } = req.body;
  // 不为空校验
  if (!id) return next({ code: 2, message: '用户id必须传递' });
  if (!oldpwd) return next({ code: 2, message: '原密码必须传递' });
  if (!newpwd) return next({ code: 2, message: '新密码必须传递' });
  if (!rnewpwd) return next({ code: 2, message: '确认密码必须传递' });
  // 正则校验  
  if (!pwdTest(oldpwd)) return next({ code: 3, message: '原密码格式错误' });
  if (!pwdTest(newpwd)) return next({ code: 3, message: '新密码格式错误' });

  next()
}

// 修改个人信息
let rinfoMid = (req, res, next) => {
  let { id, gender, age, tel } = req.body;

  // 不为空校验
  if (!id) return next({ code: 2, message: '用户id必须传递' });
  // 如果为空则不做正则校验
  if (gender && !/^(男|女|保密)$/.test(gender)) return next({ code: 3, message: '性别格式错误' });
  if (age && !ageTest(age)) return next({ code: 3, message: '年龄格式错误' });
  if (tel && !/^1\d{10}$/.test(tel)) return next({ code: 3, message: '手机号格式错误' });
  next();
}


let updateM = (req, res, next) => {
  let { uid, opt, val } = req.body;

  if (!uid) return next({ code: 0, message: '请求失败,必须传递uid' });
  if (!opt) return next({ code: 0, message: '请求失败,必须传递opt' });
  if (!/^(flag|reset)$/.test(opt)) return next({ code: 0, message: '请求失败,opt格式错误' });
  if (opt === 'flag') {    
    if (!/^(1|0)$/.test(val)) return next({ code: 0, message: '请求失败,val必须传递或格式错误' });
  }
  next();

}

let delM = (req, res, next) => {
  let { uid } = req.body;
  if (!uid) return next({
    code: 0,
    message: '请求失败,必须传递uid'
  });

  next();

}



module.exports = {
  registerMid, verifyMid, loginMid, infoMid, tokenTest, pwdMid, rinfoMid,updateM,delM
}